from os import chdir
from os.path import relpath
import tkinter as tk
import os
import pandas as pd
from PIL import Image, ImageTk

class Login(tk.Frame):
    def __init__(self, master) -> None:
        super().__init__(master)
        
        # print(os.listdir('./data/records/'))
        chdir('./data/records')
        record_list = sorted( os.listdir(), key=os.path.getmtime)
        chdir('../..')
        print(record_list)
        
        dispaly_record = None
        if (record_list[-1][0] != '.'):
            dispaly_record = record_list[-1]
        else:
            dispaly_record = record_list[-2]
        
        # image
        self.img_frame = tk.Frame(master)
        self.img_frame.pack()
        img = None
        try:
            img = Image.open('./data/records/'+dispaly_record+'/img.jpg')
            img = img.transpose(Image.ROTATE_90)
            scale = img.height / 400
            img = img.resize((int(img.width / scale), 400))
            # img.show()
            self.img = ImageTk.PhotoImage(img)
            self.img_label = tk.Label(self.img_frame, image=self.img)
            self.img_label.pack()
        except FileNotFoundError as e:
            print(e)
                
        
        
        record :pd.DataFrame= pd.read_excel('./data/records/'+dispaly_record+'/records.xlsx')
        print(record)
        
        tk.Label(master, text=str(record)).pack()
        
        self.label_and_entry_label :tk.Frame = tk.Frame(master)
        self.label_and_entry_label.pack()
        # labels
        self.label_frame :tk.Frame = tk.Frame(self.label_and_entry_label)
        self.label_frame.pack(side='left')
        tk.Label(self.label_frame, text='用户名').pack()
        tk.Label(self.label_frame, text='密码').pack()
        # entries
        self.entry_frame :tk.Frame = tk.Frame(self.label_and_entry_label)
        self.entry_frame.pack(side='left')
        self.username_entry = tk.Entry(self.entry_frame)
        self.password_entry = tk.Entry(self.entry_frame, show='*')
        self.username_entry.pack()
        self.password_entry.pack()
        
        # buttons
        self.button_frame = tk.Frame(master)
        self.button_frame.pack()
        tk.Button(self.button_frame, text="同意").pack(side='left')
        tk.Button(self.button_frame, text='拒绝').pack(side='left')
        
    
if __name__ == "__main__":
    root = tk.Tk()
    root.title('Login')
    main = Login(root)
    main.mainloop()
